<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>SB Admin</title>
    <!-- Bootstrap core CSS-->
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Custom fonts for this template-->
    <link href="/static/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <!-- Page level plugin CSS-->
    <link href="/static/vendor/datatables/dataTables.bootstrap4.css" rel="stylesheet">
    <!-- Custom styles for this template-->
    <link href="/static/css/sb-admin.css" rel="stylesheet">
</head>

<body class="fixed-nav sticky-footer bg-dark" id="page-top">
    <!-- Navigation-->
    {% include 'navbar.html' %}
    <div class="content-wrapper">
        <div class="container-fluid">
            <!-- Breadcrumbs-->
            <ol class="breadcrumb">

                <li class="breadcrumb-item active">天气数据分析</li>
            </ol>
            
            <!-- 表格 -->


                    <!-- 饼图环形图 -->
    <div class="row justify-content-center"> <!-- 添加 justify-content-center 类 -->
      <div class="col-lg-6"> <!-- 移除 text-center 类 -->
        <div class="card mb-3">
          <div class="card-header">
            <i class="fa fa-pie-chart"></i> 天气情况
          </div>
          <div class="card-body text-center"> <!-- 添加 text-center 类 -->
            <div id="pieChart" style="width: 100%; height: 400px;"></div>
          </div>
        </div>
      </div>
      <!-- 环形图 -->
      <div class="col-lg-6"> <!-- 移除 text-center 类 -->
        <div class="card mb-3">
          <div class="card-header">
            <i class="fa fa-pie-chart"></i> 风向风力
          </div>
          <div class="card-body text-center"> <!-- 添加 text-center 类 -->
            <div id="ringChart" style="width: 100%; height: 400px;"></div>
          </div>
        </div>
      </div>
    </div>
<!-- Icon Cards-->
            <div class="row">
                <!-- 搜索框 -->
                <div class="col-xl-3 col-sm-6 mb-3">
                    <ul class="nav">
                        <li class="nav-item">
                            <form id="searchForm" class="form-inline my-2 my-lg-0 mr-lg-2">
                                <div class="input-group">
                                    <input id="searchInput" class="form-control" type="text" placeholder="Search for...">
                                   <!-- <input id="searchInput2" class="form-control" type="text" placeholder="Search for...">-->
                                    <input id="date" class="form-control" type="month" placeholder="请输入日期...">
                                    <span class="input-group-btn">
                                        <button id="searchBtn" class="btn btn-primary" type="submit"> <!-- 修改按钮类型为 "submit" -->
                                            <i class="fa fa-search"></i>
                                        </button>
                                    </span>
                                </div>
                            </form>
                        </li>
                    </ul>
                </div>

            </div>
            <div class="col-lg-12">
            <div class="card mb-3">
                <div class="card-header">
                <i class="fa fa-line-chart"></i> 最高温度 vs 最低温度
                </div>
                <div class="card-body text-center">
                <div id="highestChart" style="width: 100%; height: 400px;"></div>
                </div>
            </div>
            </div>


            <!-- <div class="row justify-content-center">
                <div class="col-lg-6">
                    <div class="card mb-3">
                        <div class="card-header">
                            <i class="fa fa-pie-chart"></i> 最高温度
                        </div>
                        <div class="card-body text-center">
                            <div id="highestChart" style="width: 100%; height: 400px;"></div>
                        </div>
                    </div>
                </div>

                <div class="col-lg-6">
                    <div class="card mb-3">
                        <div class="card-header">
                            <i class="fa fa-pie-chart"></i> 最低温度
                        </div>
                        <div class="card-body text-center">
                            <div id="lowestChart" style="width: 100%; height: 400px;"></div>
                        </div>
                    </div>
                </div>
            </div> -->









        </div>
        <footer class="sticky-footer">
            <div class="container">
                <div class="text-center">
                    <small>flask+mysql天气数据分析系统</small>
                </div>
            </div>
        </footer>
        <!-- Scroll to Top Button-->
        <a class="scroll-to-top rounded" href="#page-top">
            <i class="fa fa-angle-up"></i>
        </a>
        <!-- Logout Modal-->
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel"
            aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel"></h5>
                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">×</span>
                        </button>
                    </div>
                    <div class="modal-body">真的要退出系统了吗？</div>
                    <div class="modal-footer">
                        <button class="btn btn-secondary" type="button" data-dismiss="modal">取消</button>
                        <a class="btn btn-primary" href="/loginOut">确定</a>
                    </div>
                </div>
            </div>
        </div>
        <!-- Bootstrap core JavaScript-->
        <script src="/static/vendor/jquery/jquery.min.js"></script>
        <script src="/static/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
        <!-- Core plugin JavaScript-->
        <script src="/static/vendor/jquery-easing/jquery.easing.min.js"></script>
        <!-- Custom scripts for all pages-->
        <script src="/static/js/sb-admin.min.js"></script>

        <script src="/static/vendor/echarts/echarts.min.js"></script>


        <script>
            // 监听表单提交事件
            $('#searchForm').submit(function (event) {
                // 阻止默认的表单提交行为
                event.preventDefault();

                // 获取用户输入的城市名称
                var city = $('#searchInput').val();
                // var data = $('#searchInput2').val();
                var data = $("#date").val();
                // 发送 POST 请求到后端
                $.ajax({
                    type: 'POST',
                    url: '/lishi2',  // 后端路由的地址
                    data: { city: city ,data: data},  // 发送的数据，城市名称
                    success: function (response) {
                        // 请求成功时的回调函数
                        console.log(response);  // 打印后端返回的数据

                        // 渲染四个折线图
                        renderLineChart(response);
                        // 渲染表格数据
                        // renderTable('dataTable', response.table_result);
                    },
                    error: function (xhr, status, error) {
                        // 请求失败时的回调函数
                        console.error(error);  // 打印错误信息
                    }
                });
            });

            // 渲染四个折线图函数
            // function renderLineChart(data) {
            //     // 渲染最高温度折线图
            //     renderChart('highestChart', '最高温度', data.highest);

            //     // 渲染最低温度折线图
            //     renderChart('lowestChart', '最低温度', data.lowest);

            //     // 渲染紫外线指数折线图
            //     //renderChart('visibility', '能见度', data.visibility);

            //     // 渲染湿度折线图
            //     //renderChart('humidityChart', '湿度', data.humidity);
            // }

            function renderLineChart(data) {
                renderChart('highestChart', data.highest, data.lowest);
            }

            function renderChart(containerId, highestData, lowestData) {
                var myChart = echarts.init(document.getElementById(containerId));

                var option = {
                    title: {
                        text: '最高温度 vs 最低温度'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        data: ['最高温度', '最低温度']
                    },
                    xAxis: {
                        type: 'category',
                        data: highestData.map(function (item) {
                            return item[0]; // 日期
                        })
                    },
                    yAxis: {
                        type: 'value'
                    },
                    series: [
                        {
                            name: '最高温度',
                            type: 'line',
                            data: highestData.map(function (item) {
                                return item[1];
                            }),
                            color: '#e74c3c'
                        },
                        {
                            name: '最低温度',
                            type: 'line',
                            data: lowestData.map(function (item) {
                                return item[1];
                            }),
                            color: '#3498db'
                        }
                    ]
                };

                myChart.setOption(option);
            }


            // // 渲染单个折线图函数
            // function renderChart(containerId, title, data) {
            //     // 基于准备好的dom，初始化echarts实例
            //     var myChart = echarts.init(document.getElementById(containerId));

            //     // 指定图表的配置项和数据
            //     var option = {
            //         title: {
            //             text: title
            //         },
            //         tooltip: {
            //             trigger: 'axis'
            //         },
            //         xAxis: {
            //             type: 'category',
            //             data: data.map(function (item) {
            //                 return item[0];
            //             })
            //         },
            //         yAxis: {
            //             type: 'value'
            //         },
            //         series: [{
            //             data: data.map(function (item) {
            //                 return item[1];
            //             }),
            //             type: 'line'
            //         }]
            //     };

                // 使用刚指定的配置项和数据显示图表。
            //     myChart.setOption(option);
            // }
            function renderTable(containerId, tableData) {
            var tableHtml = '<table class="table table-bordered"><thead><tr><th>观测时间</th><th>最高温度</th><th>最低温度</th><th>白天天气状况</th><th>晚间天气状况</th><th>降水量</th><th>能见度</th><th>云量</th></tr></thead><tbody>';

            // 添加表格数据
            for (var i = 0; i < tableData.length; i++) {
                var rowData = tableData[i];
                tableHtml += '<tr>';
                tableHtml += '<td>' + rowData[0] + '</td>';
                tableHtml += '<td>' + rowData[1] + '</td>';
                tableHtml += '<td>' + rowData[2] + '</td>';
                tableHtml += '<td>' + rowData[3] + '</td>';
                tableHtml += '<td>' + rowData[4] + '</td>';
                tableHtml += '<td>' + rowData[5] + '</td>';
                tableHtml += '<td>' + rowData[6] + '</td>';
                tableHtml += '<td>' + rowData[7] + '</td>';
                tableHtml += '</tr>';
            }

            tableHtml += '</tbody></table>';

            // 将表格数据添加到页面中
            document.getElementById(containerId).innerHTML = tableHtml;
        }
        </script>

    <script>



    // 使用从后端路由获取的数据设置饼图


    var pieData = JSON.parse('{{ highest_wind | tojson | safe }}');
    var pieChart = echarts.init(document.getElementById('pieChart'));
    var pieOption = {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            selectedMode: 'multiple', // 点击控制是否显示
            data: pieData.map(item => item[0])
        },
        series: [{
            name: '天气',
            type: 'pie',
            radius: '55%',
            center: ['60%', '60%'],
            data: pieData.map(item => ({ value: item[1], name: item[0] })),
            label: {
                show: true,
                formatter: '{b} : {d}%',
            },
            labelLayout: {
                hideOverlap: true
            },
            labelLine: {
                length: 10,
                length2: 10
            },
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }]
    };
    pieChart.setOption(pieOption);
</script>
<script>
    // 使用从后端路由获取的数据设置环形图
    var ringData = JSON.parse('{{ highest_humidity | tojson | safe }}');
    var ringChart = echarts.init(document.getElementById('ringChart'));
    var ringOption = {
        tooltip: {
            trigger: 'item',
            formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
            orient: 'vertical',
            left: 'left',
            selectedMode: 'multiple',
            data: ringData.map(item => item[0])
        },
        series: [{
            name: '风向 风力',
            type: 'pie',
            radius: ['50%', '70%'],
            center: ['60%', '60%'],
            data: ringData.map(item => ({ value: item[1], name: item[0] })),
            label: {
                show: true,
                formatter: '{b} : {d}%'
            },
            labelLayout: {
                hideOverlap: true
            },
            labelLine: {
                length: 10,
                length2: 10
            },
            emphasis: {
                itemStyle: {
                    shadowBlur: 10,
                    shadowOffsetX: 0,
                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                }
            }
        }]
    };
    ringChart.setOption(ringOption);
</script>



    </div>
</body>

</html>
